import sys
from pathlib import Path

# 改变路径
current_folder = Path(__file__).absolute().parent
father_folder = str(current_folder.parent)
sys.path.append(father_folder)

from utils.文件夹下的所有文件 import getFilesList, getDirsList
from shutil import copyfile
import UnityPy

REGION: dict[str] = {"en": "en-us", "jp": "ja-jp"}


def isEnFile(filePath: str) -> bool:
    print('.', end='')
    env = UnityPy.load(filePath)
    for obj in env.objects:
        obj_name = str(obj.container).split('/')[-1].split('.')[0].upper()
        region = str(obj.container).split('/')[-2] if len(
            str(obj.container).split('/')) > 1 else ''
        if obj.type.name == "TextAsset" and obj_name.startswith(
                'IDS') and region == REGION['en'] and obj_name in real_names:
            print(obj_name)
            return True
    return False


# 首先拿到所有应该拿到的real name

real_names = [name.split('.')[0] for name in getFilesList('output/IDS_To_JSON')]
print(real_names)

game_root_path = "E://Program Files (x86)//Steam//steamapps//common//Yu-Gi-Oh!  Master Duel//LocalData//LocalDataFiles//0000"  # noqa

all_dirs = getDirsList(game_root_path)

all_file_paths = []

for dir in all_dirs:
    for file in getFilesList(game_root_path + '/' + dir):
        if (isEnFile(f'{game_root_path}/{dir}/{file}')):
            all_file_paths.append(f'{game_root_path}/{dir}/{file}')

print(all_file_paths)

# 将所有文件复制到一个新文件夹里面
for path in all_file_paths:
    copyfile(path, 'data/日文IDS/' + path.split('/')[-1])
